package com.gali.juc;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * @Auther: gali
 * @Date: 2022-08-18 20:55
 * @Description:
 */
public class CompletableFutureApi2 {
    public static void main(String[] args) {
        CompletableFuture.supplyAsync(()->{
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return 1;
        }).thenApply(f->f+2).thenApply(f->f+30).whenComplete((v,e)->{
            if (e==null){
                System.out.println("计算结果："+v);
            }
        }).exceptionally(e->{
            e.printStackTrace();
            System.out.println(e.getMessage());
            return null;
        });
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(Thread.currentThread().getName()+"主线程");
    }
}
